import { instance as request, instance2 as nodeRequest } from '../request'

/**
 * 注册账号
 * @param data
 * @returns
 */
export const regUserApi = (data: regUserItf): PromiseRes =>
  request.post('/api/register', data)

/**
 * 验证账号
 * @param account
 * @returns
 */
export const verifyAccountApi = (
  account: string
): PromiseRes<verifyAccountResItf> => request.post('/api/verify', { account })

/**
 * 登录
 * @param data
 * @returns
 */
export const loginApi = (data: loginItf): PromiseRes =>
  request.post('/api/login', data)

/**
 * 用户退出
 * @returns
 */
export const logoutApi = (): PromiseRes => request.post('/user/logout')

/**
 * 验证登录状态
 * @returns
 */
export const loginStatusApi = (): PromiseRes<true | false> =>
  request.post('/user/status')

/**
 * 获取用户信息
 * @returns
 */
export const getUserInfoApi = (): PromiseRes<getUserInfoItf> =>
  request.get('/user/info')

/**
 * 修改密码
 * @returns
 */
export const modifyPasswordApi = (data: modifyPasswordItf): PromiseRes =>
  request.put('/user/modifyPassword', data)

/**
 * 更新信息
 * @param data
 * @returns
 */
export const updateInfoApi = (data: updateInfoItf): PromiseRes =>
  request.post('/user/updateInfo', data)

// /**
//  * 上传头像文件
//  * @param avatar
//  * @returns
//  */
// export const updateAvatarApi = (avatar: FormData): PromiseRes =>
//   request.post('/user/avatar', avatar, {
//     headers: {
//       'Content-Type': 'multipart/form-data',
//     },
//   })

/**
 * 上传图片文件
 * 添加一个新的表单字段，使用“file”作为键，选择一个要上传的文件。
 * let formdata = new FormData()
 * formdata.append('file', $file)
 * */
export const updateAvatarApi = (avatar: FormData): PromiseRes =>
  nodeRequest.post('/user/updateAvatar', avatar, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })

/**
 * 下载预览头像文件
 * @param path
 * @returns
 */
export const downloadFileApi = (path: string): PromiseRes =>
  request.get('/api/download', {
    params: {
      path,
    },
  })
